package com.storm.lib.base;

import java.io.IOException;
import java.net.InetSocketAddress;


import org.apache.mina.core.service.IoAcceptor;
import org.apache.mina.core.service.IoHandler;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import com.storm.lib.net.socket.codec.AMF3CodecFactory;
import com.storm.lib.type.NetType;
import com.storm.lib.util.BeanUtil;
import com.storm.lib.util.ExceptionUtil;



/**
 * 
 * 类功能:Socket服务器
 * @author johnny
 * @version 2011-8-4
 */
public class BaseSocketServer {

	/**
	 * 接受处理器
	 */
	public static IoAcceptor acceptor;

	/**
	 * 方法功能:初始化Socket服务器
	 * 更新时间:2011-8-4, 作者:johnny
	 */
	public static void initSocketServer(int[] timerTypes,Class[] timerClassz){
		 BaseInitProcessor initProcessor = (BaseInitProcessor) BeanUtil.getBean("initProcessor");
		 initProcessor.init(timerTypes,timerClassz);
		 acceptor = new NioSocketAcceptor();
	     acceptor.getFilterChain().addLast("logger", new LoggingFilter() );
	     acceptor.getFilterChain().addLast("codec",new ProtocolCodecFilter( new AMF3CodecFactory()));
	     acceptor.setHandler((IoHandler) BeanUtil.getBean("socketHandler"));
	     acceptor.getSessionConfig().setReadBufferSize(NetType.SOCKET_READ_BUFFER_SIZE);
	     acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE,NetType.SOCKET_IDLE_SECONDS);
	     try {
			acceptor.bind(new InetSocketAddress(NetType.SOCKET_PORT));
		} catch (IOException e) {
			ExceptionUtil.processException(e);
		}		
	}
	

}
